package com.getpebble.android.main.sections.notifications;

import android.content.Intent;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.PreferenceCategory;
import android.preference.PreferenceFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.getpebble.android.PebbleApplication;
import com.getpebble.android.R;
import com.getpebble.android.common.core.async.PblAsyncTask;
import com.getpebble.android.common.core.trace.Trace;
import com.getpebble.android.common.core.util.PblPreferences;
import com.getpebble.android.common.core.util.PblPreferences.PrefKey;
import com.getpebble.android.notifications.util.CoreNotifications;

public class NotificationSettingsFragment extends PreferenceFragment {
    public static final String TAG = NotificationSettingsFragment.class.getSimpleName();
    private OnPreferenceChangeListener mOnCoreNotificationChangedListener = new OnPreferenceChangeListener() {
        public boolean onPreferenceChange(Preference preference, Object newValue) {
            new RefreshAppsTask().submit();
            return true;
        }
    };
    private PblPreferences mPblPreferences;

    private class RefreshAppsTask extends PblAsyncTask {
        private RefreshAppsTask() {
        }

        public boolean doInBackground() {
            Trace.debug(TAG, "Core notification preference changed, calling update to disable if required");
            CoreNotifications.disableNotificationsForNativeApps(PebbleApplication.getAppContext());
            CoreNotifications.updateAllowedStateForNativeApps(PebbleApplication.getAppContext());
            return true;
        }

        public void onTaskSuccess() {
        }

        public void onTaskFailed() {
        }
    }

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getPreferenceManager().setSharedPreferencesName("PEBBLE_PREFERENCES");
        addPreferencesFromResource(R.xml.notification_settings);
        this.mPblPreferences = new PblPreferences(getActivity());
        findPreference(this.mPblPreferences.prefKeyToString(PrefKey.CALENDAR_NOTIFICATIONS)).setOnPreferenceChangeListener(this.mOnCoreNotificationChangedListener);
        findPreference(this.mPblPreferences.prefKeyToString(PrefKey.CALL_NOTIFICATIONS)).setOnPreferenceChangeListener(this.mOnCoreNotificationChangedListener);
        findPreference(this.mPblPreferences.prefKeyToString(PrefKey.SMS_NOTIFICATIONS)).setOnPreferenceChangeListener(this.mOnCoreNotificationChangedListener);
        PreferenceCategory emailCategory = (PreferenceCategory) findPreference(getString(R.string.pref_key_email_notifications_category));
        if (!CoreNotifications.allowGmailImapNotifications()) {
            getPreferenceScreen().removePreference(emailCategory);
        }
        findPreference(getString(R.string.pref_key_notification_help)).setIntent(new Intent("android.intent.action.VIEW", Uri.parse(PebbleApplication.getBootConfig().getSupportNotificationHelpUrl())));
    }

    public void onResume() {
        super.onResume();
        hideWearInstallItemIfRequired();
    }

    private void hideWearInstallItemIfRequired() {
        PreferenceCategory responsesCategory = (PreferenceCategory) findPreference(getString(R.string.pref_key_canned_responses));
        Preference installWearPref = findPreference(getString(R.string.pref_key_install_wear));
        if ((isWearAppInstalled() || !this.mPblPreferences.getBooleanData(PrefKey.RECEIVED_POTENTIAL_WEAR_ENHANCED_NOTIFICATION, false)) && installWearPref != null) {
            Trace.verbose(TAG, "Hiding Wear app install preference");
            responsesCategory.removePreference(installWearPref);
        }
    }

    private static boolean isWearAppInstalled() {
        try {
            PebbleApplication.getAppContext().getPackageManager().getPackageInfo("com.google.android.wearable.app", 1);
            return true;
        } catch (NameNotFoundException e) {
            return false;
        }
    }

    public void onDestroy() {
        this.mPblPreferences = null;
        super.onDestroy();
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        if (view == null) {
            Trace.error(TAG, "Failed to create view");
        } else {
            view.setBackgroundColor(getResources().getColor(R.color.preferences_background_color));
        }
        return view;
    }
}
